﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace WellDone
{
    public sealed partial class Login : Page
    {
        private bool initialized = false;
        public Login()
        {
            this.InitializeComponent();
        }

        private async void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // Disable the UI
                tbUsername.IsEnabled = false;
                pbPassword.IsEnabled = false;
                btnLogin.IsEnabled = false;
                imgLogo.Opacity = 0.5;
                tblkTitle.Opacity = 0.5;
                tblkUsername.Opacity = 0.5;
                tblkPassword.Opacity = 0.5;
                prWait.IsActive = true;
                // Attempt login...
                Globals.token = await Globals.client.LoginAsync(tbUsername.Text, pbPassword.Password);
                int currentLoginID = await Globals.client.GetLoginIDAsync(Globals.token);
                System.Collections.ObjectModel.ObservableCollection<WellDone.ServiceReferenceOpenRPOS.Login> loginCollection = await
                    Globals.client.GetLoginObjectsAsync(Globals.token, "ID=" + currentLoginID.ToString());
                // If the login was valid, go to the main display
                if (Globals.token > 0)
                {
                    if (!initialized)
                    {
                        Globals.MenuItemRecords = await Globals.client.GetMenuItemObjectsAsync(Globals.token, "Active=1 and Enabled=1 order by FullName");
                        initialized = true;
                    }
                    // Load Sent orders...
                    //await Globals.loadSentOrders();
                    // Navigate to the main page...
                    Frame rootFrame = Window.Current.Content as Frame;
                    rootFrame.Navigate(typeof(MainPage), e);
                }
                else
                {
                    // Display error message
                    tblkErrorMessage.Text = "Invalid Login";
                }
            }
            catch (Exception ex)
            {
                tblkErrorMessage.Text = "Error: Unable to login";
#if DEBUG
                System.Diagnostics.Debug.WriteLine(ex.Message);
#endif
            }
            finally
            {
                // Restore the UI
                tbUsername.IsEnabled = true;
                pbPassword.IsEnabled = true;
                btnLogin.IsEnabled = true;
                imgLogo.Opacity = 1.0;
                tblkTitle.Opacity = 1.0;
                tblkUsername.Opacity = 1.0;
                tblkPassword.Opacity = 1.0;
                prWait.IsActive = false;
            }
        }

        private void pbPassword_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            // Attempt login when the "Enter" key is pressed in the password box
            if (e.Key == Windows.System.VirtualKey.Enter)
            {
                btnLogin_Click(sender, e);
            }
        }

    }
}
